package dli_and_obs.job_utils

import java.util.Properties

import com.huawei.dli.sdk.DLIClient
import dli_and_obs._
/**
  * @Description TODO 
  * @Author liutaowei
  * @Date 2019-5-22 15:33
  */
object dli_job_state {

  def DLI_jobstate(properties: Properties):String ={
    val dli_client = dli_and_obs.utils.dli_utils.get_dli_client(properties)
    val cluster_name = properties.getProperty("cluster_name")
    val class_names = properties.getProperty("class_names")
          .split(";").toList
          .filter(""!=_).filter(null != _)

    for(job_name<-class_names){
      val state = get_project_state(dli_client,cluster_name,job_name)
      println(job_name + " 任务状态： " + state)
    }

    return "success"
  }


  def get_project_state(client:DLIClient, cluster_name:String = "o2o16u",job_name:String):String={
    import scala.collection.JavaConverters._

    var batchJobs = client.listAllBatchJobs(cluster_name,0,30).asScala
    //      .filter(_.getState.toString == "DEAD")

    val job_name =""
    if(job_name.length>3){
      batchJobs = batchJobs.filter(_.getName.equals(job_name.trim))
    }

    if(batchJobs.size<1){
      return "无查询任务"
    }else{
      val job = batchJobs(0)
      return "查询任务名称" + job.getName + "\t任务创建时间" + job.getCreateTime + "\n任务状态"+job.getState
    }
  }

}
